package com.whfc.ms.api.mach.controller.v1;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.mach.dto.obd.MaintainNumStatDTO;
import com.whfc.mach.dto.obd.MaintainRuleDTO;
import com.whfc.mach.dto.obd.ObdMaintainDTO;
import com.whfc.mach.param.MaintainHandleParam;
import com.whfc.mach.param.MaintainRuleSetParam;
import com.whfc.ms.api.mach.service.MsObdMaintainService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author sun_guodong
 * @description 设备保养
 * @date 2021-03-05
 */
@RestController
@RequestMapping("/ms/api/obd/maintain")
public class MsObdMaintainController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsObdMaintainService maintainService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam(value = "state", required = false) Integer state,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startTime", required = false) Date startTime,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endTime", required = false) Date endTime) {
        logger.info("设备保养列表,loginer:{},deptId:{},pageNum:{},pageSize:{},state:{},startTime:{},endTime:{}",
                user.getUsername(), deptId, pageNum, pageSize, state, startTime, endTime);
        PageData<ObdMaintainDTO> pageData = maintainService.list(deptId, pageNum, pageSize, state, startTime, endTime);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/numStat")
    public Result numStat(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("deptId") Integer deptId) {
        logger.info("设备保养数据统计,loginer:{},deptId:{}", user.getUsername(), deptId);
        MaintainNumStatDTO data = maintainService.numStat(deptId);
        return ResultUtil.success(data);
    }

    @GetMapping("/detail")
    public Result detail(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("maintainId") Integer maintainId) {
        logger.info("设备保养详情,loginer:{},maintainId:{}", user.getUsername(), maintainId);
        ObdMaintainDTO maintainDTO = maintainService.detail(maintainId);
        return ResultUtil.success(maintainDTO);
    }

    @PostMapping("/handle")
    public Result handle(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestBody @Validated MaintainHandleParam param) {
        logger.info("处理设备保养,loginer:{},param:{}", user.getUsername(), param.toString());
        param.setMaintainUserId(user.getId());
        param.setMaintainUserName(user.getNickname());
        maintainService.handle(param);
        return ResultUtil.success();
    }

    @PostMapping("/rule/set")
    public Result ruleSet(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestBody @Validated MaintainRuleSetParam param) {
        logger.info("设备保养触发条件设置,loginer:{},param:{}", user.getUsername(), param.toString());
        maintainService.ruleSet(param);
        return ResultUtil.success();
    }

    @GetMapping("/rule/detail")
    public Result ruleDetail(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("deptId") Integer deptId) {
        logger.info("设备保养触发条件详情,loginer:{},deptId:{}", user.getUsername(), deptId);
        ListData<MaintainRuleDTO> listData = maintainService.ruleDetail(deptId);
        return ResultUtil.success(listData);
    }

    @GetMapping("/data")
    public Result getMaintainData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("machId") Integer machId,
                                  @RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("pageSize") Integer pageSize) {
        logger.info("查找设备保养信息,loginer:{},machId:{},pageNum:{},pageSize:{}",
                user.getUsername(), machId, pageNum, pageSize);
        PageData<ObdMaintainDTO> pageData = maintainService.getMaintainData(machId, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @PostMapping("/export/{machId}")
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @PathVariable("machId") Integer machId) {
        logger.info("导出设备保养信息,loginer:{},machId:{}", user.getUsername(), machId);
        OssPathDTO ossPathDTO = maintainService.export(machId);
        return ResultUtil.success(ossPathDTO);
    }

    @GetMapping("/getNeedMaintainInfo")
    public Result getNeedMaintainInfo(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                      @RequestParam("machId") Integer machId) {
        logger.info("获取设备需要保养的信息,loginer:{},machId:{}", user.getUsername(), machId);
        ObdMaintainDTO data = maintainService.getNeedMaintainInfo(machId);
        return ResultUtil.success(data);
    }

}
